package com.gxt.sysmanage.controller;

import com.gxt.sysmanage.domain.Dictionaryitem;
import com.gxt.sysmanage.service.IDictionaryService;
import com.gxt.sysmanage.domain.Dictionary;
import com.gxt.sysmanage.query.DictionaryQuery;
import com.gxt.basic.utils.AjaxResult;
import com.gxt.basic.utils.PageList;
import com.gxt.sysmanage.service.IDictionaryitemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionary")
@Api(value = "数据字典controller",description = "数据字典controller")
public class DictionaryController {
    @Autowired
    public IDictionaryService dictionaryService;
    @Autowired
    private IDictionaryitemService dictionaryitemService;


    /**
     * 保存和修改公用的
     * @param dictionary  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @ApiOperation(value = "保存数据字典")
    public AjaxResult save(@RequestBody Dictionary dictionary){
        try {
            if( dictionary.getId()!=null)
                dictionaryService.update(dictionary);
            else
                dictionaryService.insert(dictionary);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    @DeleteMapping(value="/{id}")
    @ApiOperation(value = "删除数据字典")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            dictionaryService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	

    @GetMapping("/{id}")
    @ApiOperation(value = "获取数据字典")
    public AjaxResult loadById(@PathVariable("id")Long id)
    {
        try {
            Dictionary dictionary = dictionaryService.loadById(id);
            return AjaxResult.me().setResult(dictionary);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }



    @GetMapping
    @ApiOperation(value = "查询所有的数据字典")
    public AjaxResult getAll(){

        try {
            List< Dictionary> list = dictionaryService.loadAll();
            return AjaxResult.me().setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }



    @PostMapping
    @ApiOperation(value = "分页查询")
    public AjaxResult pageList(@RequestBody DictionaryQuery query)
    {
        try {
            PageList<Dictionary> pageList = dictionaryService.pageList(query);
            return AjaxResult.me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除数据字典")
    public AjaxResult batchDelete(@ApiParam(required = true) @RequestBody List<Long> ids){
        try {
            dictionaryService.batchDelete(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败!"+e.getMessage());
        }
        return AjaxResult.me();
    }

    @GetMapping("/item/{sn}")
    public AjaxResult getDictionaryitemBySn(@PathVariable("sn")String sn)
    {
        try {
            List<Dictionaryitem> list = dictionaryitemService.getDictionaryitemBySn(sn);
            return AjaxResult.me().setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }
}
